package com.kltn.zigo.data.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.kltn.zigo.R;
import com.kltn.zigo.data.loader.GroupCustomerLoader;

public class GroupCustomerAdapter  extends BaseAdapter{

	private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    private GroupCustomerLoader groupcustomerloader;
    ArrayList<String> selectedGroupCustomer;
    ArrayList<String> selectedGroupCustomerID;
    
    public ArrayList<String> getSelectedGroupCustomer() {
        return selectedGroupCustomer;
    }

    public ArrayList<String> getSelectedGroupCustomerID() {
        return selectedGroupCustomerID;
    }
    
    public void setSelectedGroupCustomer(ArrayList<String> selectedGroupCustomer) {
        this.selectedGroupCustomer = selectedGroupCustomer;
    }
    
    public void setSelectedGroupCustomerID(ArrayList<String> selectedGroupCustomerID) {
        this.selectedGroupCustomerID = selectedGroupCustomerID;
    }
    
    public GroupCustomerAdapter(Activity a, GroupCustomerLoader ul) {
    	selectedGroupCustomer = new ArrayList<String>();
    	selectedGroupCustomerID = new ArrayList<String>();
        activity = a;
        data=ul.GetGroupCustomer();
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        groupcustomerloader = ul;        
    }
    
    public GroupCustomerAdapter(Activity a, GroupCustomerLoader ul, ArrayList<HashMap<String, String>> d) {
    	selectedGroupCustomer = new ArrayList<String>();
    	selectedGroupCustomerID = new ArrayList<String>();
    	activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        groupcustomerloader = ul;  
    }
	
	public int getCount() {
        return data.size();
	}

	public Object getItem(int position) {
        return position;
	}

	public long getItemId(int position) {
        return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.item_list_utility, null);

        final TextView GroupCustomer_id = (TextView)vi.findViewById(R.id.tvID);
        final CheckBox GroupCustomer_name = (CheckBox)vi.findViewById(R.id.chb_Utility);
        
        HashMap<String, String> GroupCustomer = new HashMap<String, String>();
        GroupCustomer = data.get(position);
        // Setting all values in listview
        GroupCustomer_id.setText(GroupCustomer.get(groupcustomerloader.TAG_GROUP_CUSTOMER_ID));
        GroupCustomer_name.setText(GroupCustomer.get(groupcustomerloader.TAG_GROUP_CUSTOMER_NAME));
        
        GroupCustomer_name.setOnCheckedChangeListener(new OnCheckedChangeListener() {			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked)
				{
					selectedGroupCustomer.add(GroupCustomer_name.getText().toString());
					selectedGroupCustomerID.add(GroupCustomer_id.getText().toString());
				}
				else
				{
					selectedGroupCustomer.remove(GroupCustomer_name.getText().toString());
					selectedGroupCustomerID.remove(GroupCustomer_id.getText().toString());
				}
			}
		});
        
        return vi;
	}
}